package com.guojian.common.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import ch.qos.logback.core.net.SyslogOutputStream;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelUtil;

public class PoiUtils extends ExcelUtil {

	public static void easyReadTest() {
		try {
			EasyExcel.read(new FileInputStream(new File("E:/湖北省行政区划（县市）.xlsx")), new AnalysisEventListener<Map<String, Object>>() {

				List<Map<String, String>> jsonList = Lists.newArrayList();
				String filename = "";

				@Override
				public void invoke(Map<String, Object> data, AnalysisContext context) {

					System.out.println(data);

					String value2 = ObjectUtil.toString(data.get(2));

					if ("仙桃市".equals(value2)) {
						Map<String, String> map = Maps.newHashMap();
						String value4 = ObjectUtil.toString(data.get(4));
						String value7 = ObjectUtil.toString(data.get(7));
						map.put("text", value4);
						map.put("value", value7);
						if (!"null".equals(value4)) {
							jsonList.add(map);
						} else {
							filename = value7 + ".json";
						}
					}

				}

				@Override
				public void doAfterAllAnalysed(AnalysisContext context) {
					System.out.println("=====================doAfterAllAnalysed=====================");

					System.out.println(JSON.toJSONString(jsonList, true));

					System.out.println(filename);

					try {
						JSON.writeJSONString(new FileOutputStream(new File("D:/420000000000/" + filename)), jsonList, SerializerFeature.PrettyFormat);
					} catch (IOException e) {
					}

				}

			}).sheet(0).headRowNumber(0).doRead();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static void easyWriteTest() {
		List<List<String>> datas = Lists.newArrayList();
		datas.add(Arrays.asList("0", "abc", "1", "bbbb"));
		datas.add(Arrays.asList("0", "abc", "1", "bbbb"));
		datas.add(Arrays.asList("0", "abc", "1", "bbbb"));
		datas.add(Arrays.asList("0", "abc", "1", "bbbb"));
		datas.add(Arrays.asList("0", "abc", "1", "bbbb"));

		List<List<String>> headTitles = Lists.newArrayList();
		headTitles.add(Arrays.asList("姓名", "性别", "A"));
		headTitles.add(Arrays.asList("年龄", "111", "B"));

		
		EasyExcel.write(new File("E:/TEST.xlsx")).sheet("测试").head(headTitles).doWrite(datas);
	}

	public static void main(String[] args) {
		easyWriteTest();
	}

}
